﻿using System.IO;
using System.Linq;
using System.Windows.Forms;
using ASRSubsDownloader.Common;

namespace ASRSubsDownloader.FileHelper
{
    public static class FileHelper
    {
        public static NotifyIcon trayIcon;

        public static void FileCreated(object sender, FileSystemEventArgs fileSystemEventArgs)
        {
            if (Config.FileExtensions.Any(x => x.ToLower() == fileSystemEventArgs.FullPath.GetFileExtension().ToLower()))
            {
                trayIcon.ShowBalloonTip(2000, "Novo arquivo",
                                        string.Format(
                                            "O arquivo {0} foi criado. Abra a aplicação para baixar a legenda para ele.",
                                            fileSystemEventArgs.FullPath), ToolTipIcon.Info);
                var fl = new File() {FileName = fileSystemEventArgs.FullPath};
                fl.HasSubtitle = System.IO.File.Exists(fileSystemEventArgs.FullPath.GetFileNameWithOutExtension() + "srt");
                Config.FilesCache.Files.Add(fl);
            }
            else if(fileSystemEventArgs.FullPath.GetFileExtension() == "srt")
            {
                Config.FilesCache.Files.Where(
                    x =>
                    x.FileName.GetFileNameWithOutExtension() ==
                    fileSystemEventArgs.FullPath.GetFileNameWithOutExtension()).ToList().ForEach(
                        y => y.HasSubtitle = true);
            }
        }

        public static void FileDeleted(object sender, FileSystemEventArgs fileSystemEventArgs)
        {
            if (Config.FileExtensions.Any(x => x.ToLower() == fileSystemEventArgs.FullPath.GetFileExtension().ToLower()))
            {
                if (System.IO.File.Exists(fileSystemEventArgs.FullPath.GetFileNameWithOutExtension() + ".srt"))
                    System.IO.File.Delete(fileSystemEventArgs.FullPath.GetFileNameWithOutExtension() + ".srt");
            }
            else if (fileSystemEventArgs.FullPath.GetFileExtension() == "srt")
            {
                Config.FilesCache.Files.Where(
                    x =>
                    x.FileName.GetFileNameWithOutExtension() ==
                    fileSystemEventArgs.FullPath.GetFileNameWithOutExtension()).ToList().ForEach(
                        y => y.HasSubtitle = false);
            }

        }

        public static void FileRenamed(object sender, RenamedEventArgs fileSystemEventArgs)
        {
            if (Config.FileExtensions.Any(x => x.ToLower() == fileSystemEventArgs.FullPath.GetFileExtension().ToLower()))
            {
                var oldSrtName = fileSystemEventArgs.OldFullPath.GetFileNameWithOutExtension() + ".srt";
                var newSrtName = fileSystemEventArgs.FullPath.GetFileNameWithOutExtension() + ".srt";

                if (System.IO.File.Exists(oldSrtName))
                {
                    System.IO.File.Copy(oldSrtName, newSrtName);
                    System.IO.File.Delete(oldSrtName);
                }
            }
            else if (fileSystemEventArgs.FullPath.GetFileExtension() == "srt")
            {
                Config.FilesCache.Files.Where(
                    x =>
                    x.FileName.GetFileNameWithOutExtension() ==
                    fileSystemEventArgs.FullPath.GetFileNameWithOutExtension()).ToList().ForEach(
                        y => y.HasSubtitle = true);
                Config.FilesCache.Files.Where(
                    x =>
                    x.FileName.GetFileNameWithOutExtension() ==
                    fileSystemEventArgs.OldFullPath.GetFileNameWithOutExtension()).ToList().ForEach(
                        y => y.HasSubtitle = false);
            }
        }

        public static string GetFileNameWithOutExtension(this string fileName)
        {
            var lastidx = fileName.LastIndexOf(".");
            if (lastidx > -1)
                return fileName.Substring(0, lastidx);
            return fileName;
        }

        public static string GetFileExtension(this string fileName)
        {
            var lastidx = fileName.LastIndexOf(".");
            if (lastidx > -1)
                return fileName.Substring(lastidx + 1);
            return fileName;
        }
    }
}
